﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CXedu.WebSite.Code;
using CXedu.Model;
using CXedu.Business;
using System.Collections;
using Ajeelee.Core.Utils;

namespace CXedu.WebSite.WebAdmin
{
    public partial class LinkEdit : AuthPageBase
    {
        protected int id;
        protected int sort;
        Hashtable table = new Hashtable();
        protected Links info = new Links();
        ILinksBusiness linkBll = new LinksBusinessFactory().CreateLinksBusiness();

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                id = QueryParamInt32("id");
                if (id > 0)
                {
                    info = linkBll.GetById(id);
                    if (info == null)
                    {
                        info = new Links();
                        MessageBoxGo("不存在该友情链接", "LinkList.aspx");
                    }
                }
                else
                {
                    table.Clear();
                    table.Add("TopNum", 1);
                    table.Add(LinksHash.OrderField, "Sort");
                    IList<Links> linkList = linkBll.GetAllLinks(table);
                    if (linkList.Count > 0)
                    {
                        sort = linkList[0].Sort + 1;
                    }
                }

                if (IsPost)
                {
                    // 更新
                    if (id > 0)
                    {
                        initModel();
                        linkBll.Update(info);
                        Function.RemoveCacheObject("/cache/linksall/");
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("LinkList.aspx", true);

                    }
                    else // 新建
                    {
                        initModel();
                        linkBll.Insert(info);
                        Function.RemoveCacheObject("/cache/linksall/");
                        System.Threading.Thread.Sleep(500);
                        Page_GoTo("LinkList.aspx", true);
                    }
                }
            }
            catch (System.Threading.ThreadAbortException ex) { }
            catch (Exception ex)
            {
                Function.WriteErrorLog(ex);
            }
        }

        public void initModel()
        {
            if (id <= 0)
            {
                info = new Links();
            }
            info.Name = FormParam("Title");
            info.Url = FormParam("LinkUrl");
            info.Sort = FormParamInt32("Sort");
        }
    }
}